package ch.fhnw.qualiscan.application.elements;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.modals.Chart;
import ch.fhnw.qualiscan.core.helpers.Filter;
import ch.fhnw.qualiscan.core.helpers.UserSessionHelper;
import ch.fhnw.qualiscan.core.libraries.ModalLibrary;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.BaseTheme;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;
import com.vaadin.ui.Alignment;

@SuppressWarnings("serial")
public class EvaluationQuestionForm extends HorizontalLayout {
	
	public String questionText;
	public int counter;
	public int numbering;
	
	public int questionId;
	
	public Embedded flashChart;
	
	public Button detailChartButton;
	
	private ModalLibrary modalLib;
	
	private UserSessionHelper sessionHelper;
	
	public Filter filter;
	
	public VerticalLayout chartsWrap;
	public HorizontalLayout flashWrap;
	
	/**
	 * This function displays a question as a standalone question.
	 * @param isSubquestion the question
	 * @param numbering number of the question
	 * @param questionText question as a string
	 * @param questionId 
	 */
	public EvaluationQuestionForm(int counter, int numbering, String questionText, boolean isSubquestion, final Filter filter, final int questionId) {
		modalLib = new ModalLibrary();
		sessionHelper = new UserSessionHelper();
		this.counter = counter;
		this.numbering = numbering;
		this.questionText = questionText;
		this.questionId = questionId;
		
		setWidth("100%");
		setMargin(true, false, true, false);
		Label numb;
		
		chartsWrap = new VerticalLayout();
		
		if(isSubquestion == true) {
			numb = new Label(counter + "." + numbering);
			setStyleName("subquestion");
			numb.setStyleName("normalNumber");
			HorizontalLayout subquestionIndent = new HorizontalLayout();
			subquestionIndent.setWidth("18px");
			addComponent(subquestionIndent);
		} else {
			numb = new Label("" + counter);
			setStyleName("noSubquestion");
			numb.setStyleName("bigNumber");
		}
		
		// numbering
		numb.setWidth("35px");
		addComponent(numb);
		
		// questiontext
		Label questionLabel = new Label(questionText);
		addComponent(questionLabel);
		setExpandRatio(questionLabel, 1.0f);
		
		// flash chart
		flashWrap = new HorizontalLayout();
		flashWrap.addComponent(generateFlash(filter));
		flashWrap.setMargin(false);
		flashWrap.setSpacing(false);
		
		// detail chart
		detailChartButton = new Button(TM.get("evaluationdomain.detailchart"));
		detailChartButton.setIcon(Config.ICON_DETAILCHART);
		detailChartButton.setStyleName(BaseTheme.BUTTON_LINK);
		detailChartButton.addListener(new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				String url = Config.BASE_URL + "api/charts/flash/?charttype=1%26language=2%26survey=" + filter.getSurveyId() + "%26domain=%26question=" + questionId+ "%26role="+ filter.getRoleId() +"%26user="+sessionHelper.getUserSession().getId()+"%26sur="+filter.getSurId()+"";
				//System.out.println("Question big url: "+url);
				modalLib.openModal(new Chart(url, questionId, 0));
			}
		});
		
		if(filter.getRoleId() == 0) {
			detailChartButton.setVisible(true);
			chartsWrap.setHeight("55px");
		} else {
			detailChartButton.setVisible(false);
			chartsWrap.setHeight("40px");
		}
		
		chartsWrap.setSpacing(false);
		chartsWrap.setMargin(false);
		chartsWrap.setWidth("300px");
		chartsWrap.addComponent(flashWrap);
		chartsWrap.addComponent(detailChartButton);
		chartsWrap.setComponentAlignment(flashWrap, Alignment.TOP_LEFT);
		chartsWrap.setComponentAlignment(detailChartButton, Alignment.TOP_LEFT);
		
		addComponent(chartsWrap);
	}
	
	public void update(Filter filter) {
		// update detail chart status$
		if(filter.getRoleId() == 0) {
			detailChartButton.setVisible(true);
			chartsWrap.setHeight("75px");
		} else {
			detailChartButton.setVisible(false);
			chartsWrap.setHeight("40px");
		}

		flashWrap.removeAllComponents();
		flashWrap.addComponent(generateFlash(filter));
	}
	
	public Embedded generateFlash(Filter filter) {
		// http://localhost:8080/quickscan/api/charts/flash/?charttype=0&language=2&survey=surveyID&domain=domainID&question=questionID&role=roleID&user=userID
		String url = Config.BASE_URL+"api/charts/flash/?charttype=0%26language=2%26survey="+ filter.getSurveyId() +"%26domain=%26question="+ questionId +"%26role="+ filter.getRoleId() +"%26user="+sessionHelper.getUserSession().getId()+"%26sur="+filter.getSurId()+"";
		//System.out.println("Question small url: "+url);
		flashChart = new Embedded(null, new ThemeResource("../qualiscan/charts/simpleChart.swf?jsondataurl=" + url));
		flashChart.setMimeType("application/x-shockwave-flash");
		flashChart.setParameter("wmode", "opaque");
		flashChart.setHeight("40px");
		flashChart.setWidth("300px");
		return flashChart;
	}
	
}
